package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Bizlogic.CAManager;
import Model.Employee;

import com.rms.util.Constant;

/**
 * @author TONY
 *
 */
public class Login extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Login() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String username =request.getParameter("username");
		Employee employee=null;
		CAManager ca= new CAManager();
      //  EmployeeDAO ed=new EmployeeDAOImpl();
       
		employee=ca.findEmployee(username);
		
		String password=request.getParameter("password");
	
		if(employee.getPassword().equals(password))
		{
			HttpSession session=request.getSession();
			session.setAttribute("employee", employee);				
			request.getRequestDispatcher("home.jsp").forward(request, response);
		}
		else
		{
			request.getRequestDispatcher("Login.jsp").forward(request, response);
		}
			
			
		
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		String ua=request.getHeader(Constant.HEAD);
		 PrintWriter out = response.getWriter();

		if(Constant.MIDP_USER_AGENT.equals(ua))
		{
			
			String username=request.getParameter("username");
			String password=request.getParameter("password");
			Employee employee=null;
			CAManager ca= new CAManager();
	      //  EmployeeDAO ed=new EmployeeDAOImpl();       
			employee=ca.findEmployee(username);					
			if(employee.getPassword().equals(password))
			{
				HttpSession session=request.getSession();
				session.setAttribute("employee", employee);	
				out.print(Constant.LOGINSUCCESS);
				
			}
			else
			{
				out.print(Constant.LOGINFAIL);
			}
			
		}
		//request.getRequestDispatcher("Login.jsp").forward(request, response);
	
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
